package com.lmy.mallback.config;

import com.lmy.mallback.interceptor.SysInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2023/1/11 11:21
 * @title 配置虚拟图片路径映射
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/swiper/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/swiperImgs/");
        registry.addResourceHandler("/image/bigType/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/bigTypeImgs/");
        registry.addResourceHandler("/image/product/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/productImgs/");
        registry.addResourceHandler("/image/productSwiperImgs/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/productSwiperImgs/");
        registry.addResourceHandler("/image/productIntroImgs/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/productIntroImgs/");
        registry.addResourceHandler("/image/productParaImgs/**").addResourceLocations("file:/Users/lmy/IdeaProjects/项目实战/小白做毕设/springboot+vue3+小程序商城项目/mall-img/productParaImgs/");
    }

    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/adminLogin","/product/**","/bigType/**","/user/wxlogin","/weixinpay/**,/admin/user/**,/admin/order/**"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }
}